package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌表 前端控制器
 *
 * @author lan
 * @since 2023-04-18
 */
@Api(tags = "品牌表控制器")
@RestController
@RequestMapping("/admin/product") //TODO 填写基础映射URL
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;
    @Autowired
    private BaseCategoryTrademarkService baseCategoryTrademarkService;


    /**
     * 品牌分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result getBaseTrademark(@PathVariable("page") Long page,
                                   @PathVariable("limit")Long limit){
        //web层构建page对象,,业务层 也可以IPage
        IPage<BaseTrademark> trademarkPage= new Page<>(page,limit);
        trademarkPage= baseTrademarkService.getBaseTrademarkByPage( trademarkPage);
        return Result.ok(trademarkPage);
    }


    /**
     * 品牌保存
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据品牌ID查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademark(@PathVariable("id") Long id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.ok(baseTrademark);
    }

    /**
     * 更新品牌
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result deleteBaseTrademark(@PathVariable("id") Long id){
        baseTrademarkService.removeById(id);
        return Result.ok();
    }




    /**
     * //根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findTrademarkList/{category3Id}")
    public  Result getTrademarkListByCategoryId(@PathVariable("category3Id") Long category3Id){
        List<BaseTrademark> baseTrademarkList= baseTrademarkService.getBaseCategoryTrademarkList(category3Id);
        return Result.ok(baseTrademarkList);
    }

    /**
     * 根据分类ID查询该分类未关联品牌
     * @param category3Id
     * @return
     */
    @GetMapping("/baseCategoryTrademark/findCurrentTrademarkList/{category3Id}")
    public Result getCurrentTrademarkList(@PathVariable("category3Id") Long category3Id){
        List<BaseTrademark> baseTrademarkList= baseTrademarkService.getCurrentTrademarkList(category3Id);
        return  Result.ok(baseTrademarkList);
    }

    /**
     * 将品牌关联到分类
     * @param categoryTrademarkVo
     * @return
     */
    @PostMapping("/baseCategoryTrademark/save")
    public Result saveBaseCategoryTrademark(@RequestBody CategoryTrademarkVo categoryTrademarkVo){
        baseCategoryTrademarkService.saveBaseCategoryTrademark(categoryTrademarkVo);
        return Result.ok();
    }

    /**
     * 删除!!!!!!解除品牌分类关联
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @DeleteMapping("/baseCategoryTrademark/remove/{category3Id}/{trademarkId}")
    public Result deleteCategoryTrademark(@PathVariable("category3Id") Long category3Id,
                                          @PathVariable("trademarkId") Long trademarkId){
        baseCategoryTrademarkService.deleteCategoryTrademark(category3Id, trademarkId);
        return Result.ok();
    }

}
